﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.GZip;

namespace PhotoResizer
{
    class HelperClasses
    {
        private static Rectangle srcRect = new Rectangle();
        private static Rectangle destRect = new Rectangle();
        private static Color backgroundColor = Color.White;
        private static Bitmap dest1;

        public static void zipFolder(string folderpath)
        {
            // name the zip file whatever the folder is named 
            // by splitting the file path to get the folder name
            string[] sTemp = folderpath.Split('\\');
            string sZipFileName = sTemp[sTemp.Length - 1].ToString();

            //get parentfolder of the given folder
            //string parentFolder = Directory.GetParent(folderpath).ToString();

            // zip up the files
            try
            {
                string[] filenames = Directory.GetFiles(folderpath);
                // Zip up the files - From SharpZipLib Demo Code
                //using (ZipOutputStream s = new ZipOutputStream(File.Create(parentFolder + "\\" + sZipFileName + ".zip")))
                using (ZipOutputStream s = new ZipOutputStream(File.Create(folderpath + "\\" + sZipFileName + ".zip")))
                {
                    s.SetLevel(9); // 0-9, 9 being the highest compression
                    byte[] buffer = new byte[4096];
                    foreach (string file in filenames)
                    {
                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);
                            }
                            while (sourceBytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }
            }
            catch (Exception ex)
            {
                throw ex;
                //MessageBox.Show(ex.Message.ToString(), "Zip Operation Error");
            }
        }

        public static Bitmap resizeImage(Bitmap imgToResize, Size size)
        {
            //source: http://www.personalmicrocosms.com/Pages/dotnettips.aspx?c=24&t=50

            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap dest = new Bitmap(size.Width, size.Height);

            // Scale the bitmap in high quality mode.
            using (Graphics gr = Graphics.FromImage(dest))
            {
                gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                gr.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                gr.DrawImage(imgToResize, new Rectangle(0, 0, size.Width, size.Height), new Rectangle(0, 0, imgToResize.Width, imgToResize.Height), GraphicsUnit.Pixel);
                gr.Dispose();
            }

            // Copy original Bitmap's EXIF tags to new bitmap.
            foreach (PropertyItem propertyItem in imgToResize.PropertyItems)
            {
                dest.SetPropertyItem(propertyItem);
            }

            imgToResize.Dispose();
            return dest;

            /*
            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Bitmap)b;*/
        }

        public static Bitmap scaleBitmapByPercent(Bitmap src, int HieghtPg, int WidthPg)
        {
            //Source: http://blogs.msdn.com/coding4fun/archive/2006/10/31/910075.aspx

            srcRect.Width = src.Width;
            srcRect.Height = src.Height;

            destRect.Width = (int)(src.Width * WidthPg) / 100;
            destRect.Height = (int)(src.Height * HieghtPg) / 100;

            dest1 = new Bitmap(destRect.Width, destRect.Height);

            using (Graphics g = Graphics.FromImage(dest1))
            {
                Brush b = new SolidBrush(backgroundColor);
                g.FillRectangle(b, destRect);

                float sourceAspect = (float)src.Width / (float)src.Height;
                float destAspect = (float)destRect.Width / (float)destRect.Height;

                if (sourceAspect > destAspect)
                {
                    // wider than high - keep the width and scale the height  
                    destRect.Width = destRect.Width;
                    destRect.Height = (int)((float)destRect.Width / sourceAspect);
                    destRect.X = 0;
                    destRect.Y = (destRect.Height - destRect.Height) / 2;
                }
                else
                {
                    // higher than wide – keep the height and scale the width  
                    destRect.Height = destRect.Height;
                    destRect.Width = (int)((float)destRect.Height * sourceAspect);
                    destRect.X = (destRect.Width - destRect.Width) / 2;
                    destRect.Y = 0;
                }

                g.DrawImage(src, destRect, srcRect, System.Drawing.GraphicsUnit.Pixel);
                b.Dispose();
                g.Dispose();
            }
            src.Dispose();
            return dest1;
        }

        #region Jpeg Quality Functions
        //source: http://blog.paranoidferret.com/?p=11

        public static void saveJpeg(string path, Bitmap img, long quality)
        {
            try
            {
                // Encoder parameter for image quality
                EncoderParameter qualityParam =
                   new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);

                // Jpeg image codec
                ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

                if (jpegCodec == null)
                    return;

                EncoderParameters encoderParams = new EncoderParameters(1);
                encoderParams.Param[0] = qualityParam;

                img.Save(path, jpegCodec, encoderParams);
                img.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private static ImageCodecInfo getEncoderInfo(string mimeType)
        {
            // Get image codecs for all image formats
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            // Find the correct image codec
            for (int i = 0; i < codecs.Length; i++)
                if (codecs[i].MimeType == mimeType)
                    return codecs[i];
            return null;
        }
        #endregion


        /*
        private void scaleBitmap ( Bitmap dest, Bitmap src )
        {    
            destRect.Width = dest.Width;
            destRect.Height = dest.Height;

            using (Graphics g = Graphics.FromImage(dest))    
            {
                Brush b = new SolidBrush(backgroundColor);
                g.FillRectangle(b, destRect); 
                srcRect.Width = src.Width;    
                srcRect.Height = src.Height;    

                float sourceAspect = (float)src.Width / (float)src.Height; 
                float destAspect = (float)dest.Width / (float)dest.Height;  

                if (sourceAspect > destAspect) 
                {        
                    // wider than high - keep the width and scale the height  
                    destRect.Width = dest.Width;    
                    destRect.Height = (int)((float)dest.Width / sourceAspect);
                    destRect.X = 0;     
                    destRect.Y = (dest.Height - destRect.Height) / 2;     
                }        
                else     
                {        
                    // higher than wide – keep the height and scale the width  
                    destRect.Height = dest.Height;     
                    destRect.Width = (int)((float)dest.Height * sourceAspect);    
                    destRect.X = (dest.Width - destRect.Width) / 2;  
                    destRect.Y = 0;        
                }   
    
                g.DrawImage(src, destRect, srcRect, System.Drawing.GraphicsUnit.Pixel);
                g.Dispose();
            }
            //dest.Dispose();
            //src.Dispose();
        }
        

        private void scaleBitmap ( Bitmap dest, Bitmap src )
        {   
            destRect.Width = dest.Width;    
            destRect.Height = dest.Height;    
            using (Graphics g = Graphics.FromImage(dest))    
            {        
                Brush b = new SolidBrush(backgroundColor);        
                g.FillRectangle(b, destRect);        
                srcRect.Width = src.Width;        
                srcRect.Height = src.Height;        
                float sourceAspect = (float)src.Width / (float)src.Height;        
                float destAspect = (float)dest.Width / (float)dest.Height;        
                if (sourceAspect > destAspect)        
                {            
                    // wider than high heep the width and scale the height            
                    destRect.Width = dest.Width;            
                    destRect.Height = (int)((float)dest.Width / sourceAspect);            
                    destRect.X = 0;            
                    destRect.Y = (dest.Height - destRect.Height) / 2;        
                }        
                else        
                {            
                    // higher than wide – keep the height and scale the width            
                    destRect.Height = dest.Height;            
                    destRect.Width = (int)((float)dest.Height * sourceAspect);            
                    destRect.X = (dest.Width - destRect.Width) / 2;            
                    destRect.Y = 0;        
                }        
                g.DrawImage(src, destRect, srcRect, System.Drawing.GraphicsUnit.Pixel);
                g.Dispose();
            }
        }
*/

        //private Bitmap scaleBitmapByPercent(Bitmap src, int HieghtPg, int WidthPg)
        //{     
        //    srcRect.Width = src.Width;
        //    srcRect.Height = src.Height;

        //    destRect.Width = (int)(src.Width * WidthPg) / 100;
        //    destRect.Height = (int)(src.Height * HieghtPg) / 100;

        //    Image resizeImage(Image imgToResize, Size size)

        //    Size newsize = new Size(destRect.Width, destRect.Height);
        //    Image resizedImage = resizeImage(src,newsize);

        //    dest1 = new Bitmap(resizedImage);         
        //    return dest1;            
        //}

    }
}
